package com.lxq.market.servlet;

import com.google.gson.Gson;
import com.lxq.market.model.App;
import com.lxq.market.service.AppService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;

@WebServlet("/app_info")
public class AppInfoServlet extends HttpServlet {

    AppService appService=AppService.getInstance();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String appName = req.getParameter("app_name");
        if (appName==null){
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        App findByName = appService.findByAppName(appName);
        if (findByName==null){
            resp.setStatus(HttpServletResponse.SC_NOT_FOUND);
            return;
        }
        Gson gson=new Gson();
        String toJson = gson.toJsonTree(findByName).toString();
        resp.setContentType("application/json;charset=utf-8");
        resp.getOutputStream().write(toJson.getBytes(StandardCharsets.UTF_8));
    }
}
